// proxy.js
const http = require('http');
const httpProxy = require('http-proxy');

// 创建代理服务器
const proxy = httpProxy.createProxyServer({});

// 创建本地 HTTP 服务器
const server = http.createServer((req, res) => {
  console.log(`Proxying request to: ${req.url}`);

  // 设置目标服务器地址，例如：https://jsonplaceholder.typicode.com
  proxy.web(req, res, { target: 'https://www.baidu.com' });
});

// 监听错误
proxy.on('error', (err, req, res) => {
  console.error('Proxy error:', err);
  res.writeHead(500, { 'Content-Type': 'text/plain' });
  res.end('Something went wrong.');
});

// 启动代理服务器
const PORT = 8080;
server.listen(PORT, '127.0.0.1', () => {
  console.log(`Local proxy server is running at http://127.0.0.1:${PORT}`);
});